var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current_active: 0,
    active: 0,
    list: [],
    already_settlement_statistic: 0,
    not_settlement_statistic: 0,  
    current_page: 1,
    last_page: 1,
  },
  async onChange(e){
     this.setData({current_active:e.detail.index});
    await this.getData()
    await this._get_detail()
  },
  getData() {
    let urlStr = this.get_current_type({type:'home'});
    app._getNetWork({
      url:app.getNetAddresss(urlStr),
      success:resdata=>{
        let { result, msg, data: { already_settlement_statistic, not_settlement_statistic } } = resdata.data;
        if (result == 0) return app.tips(msg);
        this.setData({
          already_settlement_statistic,not_settlement_statistic
        })
      }
    })
  },
  
  get_current_type({ type }) {
    if(type == 'page'){
      return 'plugin.sign-buy.frontend.reward-detail.record-page'
    }
    if (this.data.current_active == 0) {
        return 'plugin.sign-buy.frontend.reward-detail.sign-profit';

    } else if (this.data.current_active == 1) {
        return 'plugin.sign-buy.frontend.reward-detail.recommend-award';
    } else {
        return 'plugin.sign-buy.frontend.reward-detail.allowance';
    }
  },
  change(e) {
    this.setData({active:e.detail.index})
    this._get_detail()
  },
  _get_detail(){
    let json = {
      reward_type: this.data.current_active == 0 ? 'sign_profit' : this.data.current_active == 1 ? 'recommend_award' : 'allowance'
    };
 
    let urlStr = this.get_current_type({type:'page'});
    if (this.data.active !== 0) {
      json.status = this.data.active == 1 ? 0 : 1;
    }

    app._getNetWork({
      url:app.getNetAddresss(urlStr),
      data:json,
      success:resdata=>{
        let { result, msg, data } = resdata.data;
        if (result == 0) return app.tips(msg);
        this.setData({
          list:data.data,
          current_page:data.current_page,
          last_page : data.last_page,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
   await this.getData()
   await this._get_detail();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {


    if (this.data.current_page >= this.data.last_page) {
      return;
    }
    let json = {
      reward_type: this.data.current_active == 0 ? 'sign_profit' : this.data.current_active == 1 ? 'recommend_award' : 'allowance'
    };
    let  urlStr = this.get_current_type({ type: 'page' });
    json.page = this.data.current_page + 1;
    if (this.data.active !== 0) {
      json.status = this.data.active;
    }

    app._getNetWork({
      url:app.getNetAddresss(urlStr),
      data:json,
      success:resdata=>{
        let { result, msg, data: { already_settlement_statistic, not_settlement_statistic, record } } = resdata.data;
        if (result == 0) return app.tips(msg);
        let list = this.data.list.concat(record.data);
        this.setData({
          list,
          current_page:record.current_page,
          last_page : record.last_page,
          already_settlement_statistic,
          not_settlement_statistic
        })
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})